package com.kfm.base.arrays;

import java.util.Arrays;

public class Demo04 {

    public static void main(String[] args) {

        /*
            给 arr 数组的第 3 位添加元素 6
         */
        int index = 3;
        int value = 6;
        int[] arr = {1, 2, 3, 4, 5};
//        int[] arr1 = new int[arr.length + 1]; // 1, 2, 3, 4, 4, 5
//        // 全量复制
//        System.arraycopy(arr, 0, arr1, 0, arr.length);

        int[] arr1 = Arrays.copyOf(arr, arr.length + 1);

        System.arraycopy(arr1, index, arr1, index + 1, arr1.length - index - 1);

        arr1[index] = value;
        System.out.println(Arrays.toString(arr1));

    }
}
